/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.engine.core;

import com.inspur.edp.bef.bizentity.GspBizEntityObject;
import com.inspur.edp.bef.bizentity.GspBusinessEntity;
import com.inspur.edp.bff.api.dataprocessor.MapperBEEntityInfo;
import com.inspur.edp.bff.api.dataprocessor.MapperBEPropertyInfo;
import com.inspur.edp.bff.spi.AbstractEntityPropertyMapper;
import com.inspur.edp.commonmodel.engine.api.common.CMEngineUtil;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import com.inspur.edp.formserver.viewmodel.GspViewModel;
import com.inspur.edp.formserver.viewmodel.GspViewModelElement;
import com.inspur.edp.formserver.viewmodel.GspViewObject;
import com.inspur.edp.metadata.rtcustomization.api.CustomizationRtService;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

public class BffEnginePropertyMapper extends AbstractEntityPropertyMapper {

  private GspViewModel vo;
  private GspBusinessEntity be;
  public BffEnginePropertyMapper(GspViewModel vo,GspBusinessEntity be){
    this.vo=vo;
    this.be=be;
  }
  @Override
  protected MapperBEEntityInfo buildBEInfo() {

    return new MapperBEEntityInfo(be.getGeneratedConfigID(),be.findObjectById(vo.getMainObject().getMapping().getTargetObjId()).getCode());
  }

  @Override
  protected void buildBEPropertyInfoFromVO() {
    String beType=be.getGeneratedConfigID();
    GspViewObject voNode=vo.getMainObject();
    GspBizEntityObject beNode= (GspBizEntityObject) be.findObjectById(vo.getMainObject().getMapping().getTargetObjId());
    voNode.getContainElements().stream().map(item -> ((GspViewModelElement) item))
        .filter(item -> !item.getIsVirtual() && be.getID()
            .equals(item.getMapping().getTargetMetadataId()))
        .forEach(item -> {
          addBEPropertyNameFromVO(item.getCode(),new MapperBEPropertyInfo(beType,beNode.findElement(item.getMapping().getTargetObjId()).getLabelID()));

        });
  }

  @Override
  protected void buildMapperByVOCode() {
    CustomizationRtService mdService = SpringBeanUtils.getBean(CustomizationRtService.class);

    for(IGspCommonObject object:vo.getAllObjectList()){
      String beId = vo.getMapping().getTargetMetadataId();
      GspBusinessEntity businessEntity = CMEngineUtil.getMetadataContent(beId);
      addMapper(((GspViewObject)object).getCode(),new BffEnginePropertyMapper(vo,businessEntity));
    }

  }

  @Override
  public String getVOCode() {
    return vo.getMainObject().getCode();
  }
}
